package com.zxy.ziems.server.energy.task;

import com.zxy.ziems.server.energy.service.DataIncrementService;
import com.zxy.ziems.server.sync.enums.SyncMetaType;
import com.zxy.ziems.server.sync.service.SyncMetaService;
import com.zxy.ziems.server.utils.AssertUtil;
import com.zxy.ziems.server.utils.RedissonLockUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

import static com.zxy.ziems.server.constant.RedisKeyLockConstant.DEVICE_MEASURE_DATA_DAY_KEY;
import static com.zxy.ziems.server.constant.RedisKeyLockConstant.TMP_DEVICE_MEASURE_DATA_HOUR_KEY_;

/**
 * @Copyright (C),
 * @ClassName: DataIncrementTask
 * @Author: yanzhiwei
 * @Description:
 * @Date: 2024/4/8 10:59
 */
@Component
@Slf4j
public class DataIncrementTask {
    @Autowired
    private DataIncrementService dataIncrementService;
    @Autowired
    private SyncMetaService syncMetaService;
    @Autowired
    private RedissonLockUtil redissonLockUtil;

    /**
     * 天数据统计
     */
    @Scheduled(cron = "0 6 0 * * ?")
    public void syncDeviceMeasureDataDay() {
        log.info("=== syncDeviceMeasureDataDay 天数据统计开始 ===");
        String key = DEVICE_MEASURE_DATA_DAY_KEY;

        RLock lock = redissonLockUtil.lock(key);
        lock.lock();

        try {
            // 查看当天任务是否执行
            LocalDateTime syncTime = syncMetaService.getSyncTime(SyncMetaType.DEVICE_MEASURE_DATA_DAY.getCode());
            AssertUtil.nonNull(syncTime, "未配置执行任务");
            LocalDate date = syncTime.toLocalDate().plusDays(1);
            LocalDate yesterday = LocalDate.now().plusDays(-1);
            while (true) {
                if (!(date.compareTo(yesterday) < 1)) {
                    break;
                }
                date = dataIncrementService.syncDeviceMeasureDataDay(date);
            }
            log.info("=== syncDeviceMeasureDataDay finally 天数据统计结束 ===");
        } catch (Exception e) {
            log.error("syncDeviceMeasureDataDay err=", e);
            throw new RuntimeException(e);
        } finally {
            log.info("unlock");
            redissonLockUtil.unlock(key);
            log.info("=== syncDeviceMeasureDataDay finally 天数据统计结束 ===");
        }
    }

    /**
     * 初始化小时数据
     * 小时执行
     */
    @Scheduled(cron = "0 5 0/1 * * ?")
    public void insertDeviceHourData() {
        log.info("=== insertDeviceHourData 小时数据入库启动 ===");
        String key = TMP_DEVICE_MEASURE_DATA_HOUR_KEY_;
        RLock lock = redissonLockUtil.lock(key);
        lock.lock();
        try {
            // 查看当天任务是否执行
            LocalDateTime syncTime = syncMetaService.getSyncTime(SyncMetaType.DEVICE_MEASURE_DATA_INCREMENT_HOUR.getCode());
            AssertUtil.nonNull(syncTime, "未配置执行任务");
            LocalDateTime date = syncTime.plusHours(1);
            LocalDateTime yesterday = LocalDateTime.now();
            while (true) {
                if (!(date.compareTo(yesterday) < 1)) {
                    break;
                }
                date = dataIncrementService.syncDeviceMeasureDataHour(date);
            }
            log.info("=== insertDeviceHourData 小时数据入库结束 ===");
        } catch (Exception e) {
            log.error("执行异常，error=", e);
        } finally {
            lock.unlock();
        }
    }

}
